﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Engine
{
    public static class ScreenManager
    {
        static List<GameScreen> screens = new List<GameScreen>();
        static public ContentManager Content;//convertir un png en un texture 2d
        static public GraphicsDeviceManager Graphics;

        static AudioEngine aEngine;
        static WaveBank waveBank;
        static SoundBank soundBank;

        public static void LoadContent(ContentManager content)
        {
            Content = content;

            aEngine = new AudioEngine("content/AudioTest.xgs");
            waveBank = new WaveBank(aEngine, "content/Wave Bank.xwb");
            soundBank = new SoundBank(aEngine, "content/Sound Bank.xsb");
        }
        public static void UnloadContent() 
        {
            foreach (GameScreen gm in screens)
                gm.UnloadContent();
        }
        public static void LoadGraphicsDevManager(GraphicsDeviceManager graphicsManager)
        {
            Graphics = graphicsManager;
        }

        public static GraphicsDeviceManager GraphicsDevice() { return Graphics; }

        public static void PlaySound(string sound)
        {
            soundBank.PlayCue(sound);
        }

        public static void AddScreen(GameScreen add)
        {
            screens.Add(add);
            add.LoadContent();
        }
       
        public static void RemoveScreen(GameScreen remove)
        {
            screens.Remove(remove);
        }
        public static void Update(GameTime gametime)//Solo le hacemos update al ultimo elemento
        {
            //Cargar antes los datos al nuevo input
            Input.LoadNewData();
           
            if (screens.Count > 0)
            { 
                Input.Update(gametime);
                screens[screens.Count - 1].Update(gametime);
                screens[screens.Count - 1].HandleInput();
            }

            //A lo ultimo poner los datos al viejo input.
            Input.SaveOldData();
        }

        public static void Draw(SpriteBatch spriteBatch, GameTime gametime)
        {
            screens[screens.Count - 1].Draw(spriteBatch, gametime);
        }
    }
}
